package com.liuyutao.controller;

import com.liuyutao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    @Qualifier("studentServiceImpl")
    private StudentService studentService;

    @RequestMapping("/main")
    public String toList() {
        return "allStudent";
    }

    @RequestMapping("/toLogin")
    public String login() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password, Model model) {
        //把用户的信息存在session中
        session.setAttribute("userLoginInfo", username);
        model.addAttribute("password", password);
        model.addAttribute("username", username);

        if (studentService.findStudentByName(username) != null && password.equals(studentService.findStudentByName(username).getNumber())) {
            return "allStudent";
        } else if (studentService.findStudentByName(username) != null && !password.equals(studentService.findStudentByName(username).getNumber())) {
            session.setAttribute("isLogin", false);
        }
        return "fail";

    }

    @RequestMapping("/goOut")
    public String goOut(HttpSession session) {
        session.removeAttribute("userLoginInfo");
        return "login";
    }
}


